// pages/component/selectUser/selectUser.js
import ajax from '../../network/index'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    userID:{
      type:Array
    }
  },
  
  /**
   * 组件的初始数据
   */
  data: {
    selectData:[],
    allUser:[],
    allStatus:[]
  },

  /**
   * 组件的方法列表
   */
  methods: {
    sumit(){
      let _this=this
      wx.showModal({
        title:"提示",
        content:"是否添加所选的员工"
        ,
        success(e){
          if(e.confirm){
_this.triggerEvent('submit',_this.data.selectData)
          }else{
            
          }
        }
      })
    },
    select(e){
      let id = e.currentTarget.dataset.id
      let cheked=e.currentTarget.dataset.cheked
      let i=e.currentTarget.dataset.index
      let all =this.data.allUser
      all[i].cheked=!cheked
      let arr =this.data.selectData
      console.log(all[i])
      let index=arr.indexOf(id)
      if(index=='-1'){
        arr.push(`${id}`)
      }else{
        arr.splice(index,index)
      }
      this.setData({
        selectData:arr,
        allUser:all
      })
      console.log(this.data.selectData,id)
    },
    close(){
      this.triggerEvent('close')
    },    
     getUserlist(){
          ajax({
              url:"/api/user/personnelList",
              method:"post"
          }).then(v=>{
              if(v.code!=200){
                wx.showToast({
                  title: v.msg,
                })
                return
              }
              let arr =v.data.content
              console.log(this.data.userID)
              let someUser=this.data.userID
              arr.forEach(v=>{
                
                  someUser.forEach(item=>{
                    
                    if(v.userId==item){
                      console.log(11)
                      v.cheked=true
                    }
                  })
                
              })
              console.log(arr)
              this.setData({
                'allUser':arr
              })
          })
  }
  },
  lifetimes:{
    attached:function(){
      this.setData({
        selectData:this.data.userID
      })
      wx.pageScrollTo({
        scrollTop:0
      })
      this.getUserlist()
    }
  }
})
